Online-Academy
Look, Read, Understand, Apply

Some Program Codes - ii

Program 1: To check if user provided number is prime or not

import java.util.Scanner;
class primeCheck{
    public static void main(String[] arr){
        int x,i;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a Number: ");
        x = sc.nextInt();
        if(x % 2 == 0){
            System.out.println("Not a prime");
            System.exit(0);
        }
        for(i=2;i<x;i++){
            if(x % i == 0){
                break; 
            }
        }
        if(x == i){
            System.out.println("It is a prime");
        }else{
            System.out.println("It is not a prime");
        }
    }
}

Program 2: Display user provided string in reverse

import java.util.Scanner;
class ReverseAString{
    public static void main(String[] arr){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String: ");
        String str = sc.next();
        StringBuffer s = new StringBuffer(str); 
        System.out.println(str+ "'s reverse is "+s.reverse());
    }
}

Program 3: Display square of numbers that lie between user provided two numbers

import java.util.Scanner;
class Square_of_nums_between{
    public static void main(String[] arr){
        int x,y,i;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First Number: ");
        x = sc.nextInt();
        System.out.println("Enter Second Number: ");
        y = sc.nextInt();
        if(x < y){
            for(i=x;i<y;i++){
                System.out.print(" "+(i*i));
            }
        }else{
            for(i=y;i<x;i++){
                System.out.print(" "+(i*i));
            }
        }
    }
}

Check if given string is palindrome or not

import java.util.Scanner;
class CheckPalindrome{
    public static void main(String[] arr){
        int i;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = sc.next();
        StringBuffer ss = new StringBuffer(s);
        if(s.equals(ss.reverse())){
            System.out.print("Provided string is a palindrome");
        }else{
            System.out.print("Provided string is not a palindrome");
        }
    }
}

Display reverse of a user provided number

import java.util.Scanner;
class ReverseANumber{
    public static void main(String[] arr){
        int i;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        i = sc.nextInt();
        int num_of_digits = (int)Math.log10(i)+1;
        for(int j=1;j<=num_of_digits;j++){
            System.out.print((i%10));
            i = i / 10;
        }
    }
}

Display factorial of a user provided number

import java.util.Scanner;
class Factorial{
    public static void main(String[] arr){
        int n,f=1;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        n = sc.nextInt();
        for(int i = 1;i<=n;i++){
            f = f * i;
        }
        System.out.print("Factorial of "+n+" is: "+f);
    }
}

Display multiplication table of a user provided number

import java.util.Scanner;
class MultiplicationTable{
    public static void main(String[] arr){
        int n,i=1;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        n = sc.nextInt();
        for(i = 1;i<=n;i++){
            System.out.println(i+" * "+n+" = " +(i * n));    
        }
        
    }
}

Display Sum of natural numbers up to a user provided number

import java.util.Scanner;
class SumOfNaturalNumbers{
    public static void main(String[] arr){
        int n,i=1,sum = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        n = sc.nextInt();
        for(i = 1;i<=n;i++){
            sum += i; 
        }
        System.out.println("Sum of number from 1 to given number "+n+" is = "+sum);    
    }
}

Display Maximum value from a integer list.

import java.util.Scanner;
class MaxOfList{
    public static void main(String[] arr){
        int n,i,position = 0;
        int[] list = {22,33,44,155,66,77,88,1,2,20,19};
        n = list.length;
        int max = list[0];
        for(i = 1;i<n;i++){
            if(max < list[i]){
                max = list[i];
                position = i;
            }
        }
        System.out.println("Maximum value in the given list is: "+max+" and position is: "+position);    
    }
}